/**
 * 证据实体类
 * 定义AI侦探应用中的证据数据结构
 */
export class Evidence {
  // 证据ID
  id: string;
  
  // 证据名称
  name: string;
  
  // 证据描述
  description: string;
  
  // 所属案件ID
  caseId: string;
  
  // 收集者ID
  collectorId: string;
  
  // 收集时间
  collectedAt: Date;
  
  // 证据类型
  evidenceType: 'document' | 'photo' | 'video' | 'audio' | 'physical' | 'other';
  
  // 证据状态
  status: 'collected' | 'analyzed' | 'verified' | 'questioned';
  
  // 存储路径
  storagePath: string;
  
  // 文件大小
  fileSize?: number;
  
  // 收集地点
  collectionLocation?: string;
  
  // 相关人员ID
  relatedPersonId?: string;
  
  // 证据标签
  tags: string[];
  
  // 证据链ID
  chainOfCustodyId?: string;
  
  // AI分析结果
  aiAnalysis?: {
    authenticityScore: number;
    keyElements: string[];
    analysisSummary: string;
  };
  
  /**
   * 构造函数
   * @param evidenceData 证据数据
   */
  constructor(evidenceData: {
    id?: string;
    name: string;
    description: string;
    caseId: string;
    collectorId: string;
    collectedAt?: Date;
    evidenceType: 'document' | 'photo' | 'video' | 'audio' | 'physical' | 'other';
    status?: 'collected' | 'analyzed' | 'verified' | 'questioned';
    storagePath: string;
    fileSize?: number;
    collectionLocation?: string;
    relatedPersonId?: string;
    tags?: string[];
    chainOfCustodyId?: string;
    aiAnalysis?: {
      authenticityScore: number;
      keyElements: string[];
      analysisSummary: string;
    };
  }) {
    this.id = evidenceData.id || this.generateId();
    this.name = evidenceData.name;
    this.description = evidenceData.description;
    this.caseId = evidenceData.caseId;
    this.collectorId = evidenceData.collectorId;
    this.collectedAt = evidenceData.collectedAt || new Date();
    this.evidenceType = evidenceData.evidenceType;
    this.status = evidenceData.status || 'collected';
    this.storagePath = evidenceData.storagePath;
    this.fileSize = evidenceData.fileSize;
    this.collectionLocation = evidenceData.collectionLocation;
    this.relatedPersonId = evidenceData.relatedPersonId;
    this.tags = evidenceData.tags || [];
    this.chainOfCustodyId = evidenceData.chainOfCustodyId;
    this.aiAnalysis = evidenceData.aiAnalysis;
  }
  
  /**
   * 生成唯一ID
   * @returns 唯一ID字符串
   */
  private generateId(): string {
    return `evidence_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }
  
  /**
   * 更新证据状态
   * @param status 新状态
   */
  updateStatus(status: 'collected' | 'analyzed' | 'verified' | 'questioned'): void {
    this.status = status;
  }
  
  /**
   * 添加标签
   * @param tag 标签
   */
  addTag(tag: string): void {
    if (!this.tags.includes(tag)) {
      this.tags.push(tag);
    }
  }
  
  /**
   * 设置AI分析结果
   * @param analysis AI分析结果
   */
  setAiAnalysis(analysis: {
    authenticityScore: number;
    keyElements: string[];
    analysisSummary: string;
  }): void {
    this.aiAnalysis = analysis;
  }
  
  /**
   * 转换为JSON对象
   * @returns JSON格式的证据数据
   */
  toJSON() {
    return {
      id: this.id,
      name: this.name,
      description: this.description,
      caseId: this.caseId,
      collectorId: this.collectorId,
      collectedAt: this.collectedAt.toISOString(),
      evidenceType: this.evidenceType,
      status: this.status,
      storagePath: this.storagePath,
      fileSize: this.fileSize,
      collectionLocation: this.collectionLocation,
      relatedPersonId: this.relatedPersonId,
      tags: this.tags,
      chainOfCustodyId: this.chainOfCustodyId,
      aiAnalysis: this.aiAnalysis
    };
  }
  
  /**
   * 从JSON对象创建Evidence实例
   * @param json JSON格式的证据数据
   * @returns Evidence实例
   */
  static fromJSON(json: any): Evidence {
    return new Evidence({
      id: json.id,
      name: json.name,
      description: json.description,
      caseId: json.caseId,
      collectorId: json.collectorId,
      collectedAt: json.collectedAt ? new Date(json.collectedAt) : new Date(),
      evidenceType: json.evidenceType,
      status: json.status,
      storagePath: json.storagePath,
      fileSize: json.fileSize,
      collectionLocation: json.collectionLocation,
      relatedPersonId: json.relatedPersonId,
      tags: json.tags || [],
      chainOfCustodyId: json.chainOfCustodyId,
      aiAnalysis: json.aiAnalysis
    });
  }
}